package cn.dm.example.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 对象锁
 * 1、synchronized修饰普通方法(注:修饰静态方法是类锁)
 * 2、synchronized对普通属性或"this"加锁(注:对静态属性或"类名.class"加锁是类锁)
 */
@Slf4j
public class SynchronizedExample1 {

    // 修饰一个代码块(作用于调用的对象)
    public void test1(int j) {
        synchronized (this) {
            for (int i = 0; i < 10; i++) {
                log.info("test1 {} - {}", j, i);
            }
        }
    }

    // 修饰一个方法(作用于调用的对象)
    public synchronized void test2(int j) {
        for (int i = 0; i < 10; i++) {
            log.info("test2 {} - {}", j, i);
        }
    }

    public static void main(String[] args) {
        SynchronizedExample1 example1 = new SynchronizedExample1();
        SynchronizedExample1 example2 = new SynchronizedExample1();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(() -> {
            example1.test1(1);  // 测试synchronized关键字修饰代码块、且对非静态属性加锁
            // example1.test2(1); // 测试synchronized关键字修饰普通方法(即非静态方法)
        });
        executorService.execute(() -> {
            example2.test1(2);
            // example2.test2(2);
        });

        executorService.shutdown();
    }
    /*
     * 09:15:00.160 [pool-1-thread-1] INFO cn.dm.example.sync.SynchronizedExample1 - test1 1 - 0
     * 09:15:00.164 [pool-1-thread-1] INFO cn.dm.example.sync.SynchronizedExample1 - test1 1 - 1
     * 09:15:00.164 [pool-1-thread-1] INFO cn.dm.example.sync.SynchronizedExample1 - test1 1 - 2
     * 09:15:00.164 [pool-1-thread-1] INFO cn.dm.example.sync.SynchronizedExample1 - test1 1 - 3
     * 09:15:00.164 [pool-1-thread-1] INFO cn.dm.example.sync.SynchronizedExample1 - test1 1 - 4
     * 09:15:00.164 [pool-1-thread-1] INFO cn.dm.example.sync.SynchronizedExample1 - test1 1 - 5  注意此处有交替
     * 09:15:00.160 [pool-1-thread-2] INFO cn.dm.example.sync.SynchronizedExample1 - test1 2 - 0  注意此处有交替
     * 09:15:00.164 [pool-1-thread-1] INFO cn.dm.example.sync.SynchronizedExample1 - test1 1 - 6
     * 09:15:00.164 [pool-1-thread-1] INFO cn.dm.example.sync.SynchronizedExample1 - test1 1 - 7
     * 09:15:00.164 [pool-1-thread-2] INFO cn.dm.example.sync.SynchronizedExample1 - test1 2 - 1
     * 09:15:00.164 [pool-1-thread-1] INFO cn.dm.example.sync.SynchronizedExample1 - test1 1 - 8
     * 09:15:00.164 [pool-1-thread-2] INFO cn.dm.example.sync.SynchronizedExample1 - test1 2 - 2
     * 09:15:00.164 [pool-1-thread-1] INFO cn.dm.example.sync.SynchronizedExample1 - test1 1 - 9
     * 09:15:00.164 [pool-1-thread-2] INFO cn.dm.example.sync.SynchronizedExample1 - test1 2 - 3
     * 09:15:00.164 [pool-1-thread-2] INFO cn.dm.example.sync.SynchronizedExample1 - test1 2 - 4
     * 09:15:00.164 [pool-1-thread-2] INFO cn.dm.example.sync.SynchronizedExample1 - test1 2 - 5
     * 09:15:00.164 [pool-1-thread-2] INFO cn.dm.example.sync.SynchronizedExample1 - test1 2 - 6
     * 09:15:00.164 [pool-1-thread-2] INFO cn.dm.example.sync.SynchronizedExample1 - test1 2 - 7
     * 09:15:00.164 [pool-1-thread-2] INFO cn.dm.example.sync.SynchronizedExample1 - test1 2 - 8
     * 09:15:00.164 [pool-1-thread-2] INFO cn.dm.example.sync.SynchronizedExample1 - test1 2 - 9
     *
     * 由上面运行结果可知,对象锁只对对象生效(即example1或者example2),保证对象内部按序执行,而对象之间是可以交替执行的
     * 即上面的结果可知,对象example1拿到锁(即synchronized (this))执行同步代码块输出0到9的途中,对象example2也能拿到锁进行输出
     *
     * 理解:example1和example2这两个对象,拥有各自的方法test1和test2,各自互不影响,所以不会出现线程等待的情况
     */
}
